package com.jee.ssm.modules.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.common.utils.UUIDFactory;

import com.jee.ssm.model.Banner;
import com.jee.ssm.model.Banner_Class;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.modules.ssm.services.BannerService;
import com.jee.ssm.modules.ssm.services.Banner_ClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/banner")
public class BannerController extends AdminBaseController<Banner>{
    /**
     * 进入用户添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("banner:add")
    public ModelAndView add(ModelMap model,HttpServletRequest request,@RequestParam Integer page, @RequestParam Integer size){
        ParamMap paramMap = new ParamMap(request);
        PageInfo<Banner_Class> pageInfo =  banner_classService.list(paramMap,page,size);

        paramMap.put("pageInfo",pageInfo);
        model.put("longId", UUIDFactory.getStringId());
//        model.put("data",bannerService.selectById1());//查询所有t_banner_class类中的分类信息 暂时无法实现，报错待改
        return new ModelAndView("manager/banner/add",model);
    }
    /**
     * 进入用户编辑页面
     * @param model 返回用户的容器
     * @param id 用户id
     * @return 编辑页面
     * @throws Exception 进入编辑页面失败
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("banner:edit")
    public ModelAndView edit(ModelMap model, @RequestParam String id) {
        model.put("data",bannerService.selectById(id));
        return new ModelAndView("manager/banner/edit",model);
    }
    /**
     * 角色添加
     * @param banner 带id的用户对象
     * @return 成功状态
     * @throws Exception 数据添加异常
     */
    @ResponseBody
    @RequestMapping(value="/save")
    @AdminControllerLog(description = "用户添加" )
    @RequiresPermissions("user:add")
    public Tip save(Banner banner, HttpServletRequest request) throws Exception{

                //临时文件
                File file = new File(banner.getBannerurl());
                //图片地址 相对路径
                String path = "/images/fileurl/"
                        + System.currentTimeMillis()+"_"
                        + "url"
                        + file.getName().substring(file.getName().lastIndexOf("."));
                //图片地址 绝对路径
                String realPath = request.getServletContext().getRealPath(path);
                //保存图片
                ImageIO.write(ImageIO.read(file), "jpg", new File(realPath));
                banner.setBannerurl(realPath);
                bannerService.insert(banner);
                return new Tip();
    }

    /**
     * 根据 id 修改用户
     * @param banner 带id的用户对象
     * @return 成功状态
     * @throws Exception 数据修改异常
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @AdminControllerLog(description = "用户编辑" )
    @RequiresPermissions("banner:edit")
    public Tip update(Banner banner,HttpServletRequest request) throws Exception {

                File file = new File(banner.getBannerurl());
                //图片地址 相对路径
                String path = "/images/fileurl/"
                        + System.currentTimeMillis()+"_"
                        +"url"
                        + file.getName().substring(file.getName().lastIndexOf("."));
                //图片地址 绝对路径
                String realPath = request.getServletContext().getRealPath(path);
                ImageIO.write(ImageIO.read(file), "jpg", new File(realPath));
                banner.setBannerurl(realPath);
                bannerService.updateById(banner);
                return new Tip();
    }
    /**
     * 根据 id 删除用户
     * @param id 用户id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @AdminControllerLog(description = "删除用户" )
    @RequiresPermissions("banner:delete")
    public Tip delete(@RequestParam String id) {

        try {
            bannerService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1);
        }
    }
    /**
     * 根据 id 列表批量删除
     * @param ids 用户id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @AdminControllerLog(description = "批量删除用户" )
    @RequiresPermissions("banner:delete")
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            bannerService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1);
        }
    }
    /**
     * 根据 id 查找用户
     * @param id 用户id
     * @return 用户对象 jsonj
     * @throws Exception 查找用户失败
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    public Banner find(@RequestParam String id) {

        return bannerService.selectById(id);
    }

    /**
     * 获取用户列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 用户列表页面
     * @throws Exception 获取用户列表失败
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("banner:list")
    public ModelAndView list(HttpServletRequest request,@RequestParam Integer page, @RequestParam Integer size) throws Exception {

        ParamMap paramMap = new ParamMap(request);
        PageInfo<Banner> pageInfo =  bannerService.list(paramMap,page,size);

        paramMap.put("pageInfo",pageInfo);
        return new ModelAndView("manager/banner/list",paramMap);
    }





    //----------------------------- property ----------------------------------
    @Resource
    private BannerService bannerService;
    @Resource
    private Banner_ClassService banner_classService;
}
